package com.arpa.wms.controller;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.consts.CommonConst;
import com.arpa.wms.domain.dto.LocationBearTypeDTO;
import com.arpa.wms.domain.entity.LocationBearType;
import com.arpa.wms.domain.enums.StatusEnum;
import com.arpa.wms.domain.vo.LocationBearTypeVO;
import com.arpa.wms.domain.vo.WarehouseVO;
import com.arpa.wms.service.ILocationBearTypeService;
import com.arpa.wms.service.ILocationService;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 库位承载类型 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-03
 */
@Api(tags = "库位承载类型")
@RestController
@RequestMapping(value="/locationBearType", produces = "application/json;charset=UTF-8")
@Log4j2
public class LocationBearTypeController  {

   private final ILocationBearTypeService  iLocationBearTypeService;

   public LocationBearTypeController(ILocationBearTypeService iLocationBearTypeService){
       this.iLocationBearTypeService = iLocationBearTypeService;
   }

    /**
     * 获取 库位承载类型 列表
     *
     * @param locationBearTypeDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 库位承载类型 列表", notes = "获取 库位承载类型 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = LocationBearTypeVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(LocationBearTypeDTO locationBearTypeDTO,HttpServletRequest request) {
        try {
            //过滤所属机构
            locationBearTypeDTO.setGroupCode(UserUtil.getBranchCode());
            LocationBearTypeVO locationBearTypeVOSum = iLocationBearTypeService.queryListSum(locationBearTypeDTO);
            List<LocationBearTypeVO> locationBearTypeVOList = iLocationBearTypeService.queryList(locationBearTypeDTO);

            Page returnPage = new Page(locationBearTypeDTO.getPageNum(),locationBearTypeDTO.getPageSize());
            returnPage.setRecords(locationBearTypeVOList);
            returnPage.setTotal(locationBearTypeVOSum.getTotal());
            returnPage.setSumObject(locationBearTypeVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 库位承载类型
     *
     * @param entity LocationBearType
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 库位承载类型 ", notes = "添加 库位承载类型 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(@Validated(LocationBearType.Save.class) LocationBearType entity, HttpServletRequest request) {
        try {
            entity.setGroupCode(UserUtil.getBranchCode());
            entity.setStatus(StatusEnum.UNACTIVE.getValue());
            Result validaResult = iLocationBearTypeService.validBearType(entity, CommonConst.ADD);
            if(!validaResult.isOk()){
                return validaResult;
            }
            iLocationBearTypeService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取库位承载类型详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取库位承载类型详情", notes = "获取库位承载类型详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "库位承载类型code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = LocationBearType.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result locationBearType(@PathVariable String code, HttpServletRequest request) {
        try {
            LocationBearType entity = iLocationBearTypeService.getByCode(code);
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改库位承载类型
     *
     * @param entity 库位承载类型
     * @param request
     * @return
     */
    @ApiOperation(value = "修改库位承载类型", notes = "修改库位承载类型")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(@Validated(LocationBearType.Update.class)LocationBearType entity, HttpServletRequest request) {
        if(StrUtil.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }

        Result validaResult = iLocationBearTypeService.validBearType(entity,CommonConst.UPDATE);
        if(!validaResult.isOk()){
            return validaResult;
        }

        int updateNum = iLocationBearTypeService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 库位承载类型
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 库位承载类型 ", notes = "删除 库位承载类型 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "库位承载类型code", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            return iLocationBearTypeService.deleteByCode(codes);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 启用库位承载类型
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "启用库位承载类型", notes = "启用库位承载类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("/active")
    public Result active(@RequestParam List<String> codes, HttpServletRequest request) {
        int updateNum = iLocationBearTypeService.activeStatus(codes);
        int failNum = codes.size() - updateNum;
        return Result.ok("启用成功" + updateNum + "条；失败" + failNum + "条；");
    }

    /**
     * 停用库位承载类型
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "停用库位承载类型", notes = "停用库位承载类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping("/unactive")
    public Result unactive(@RequestParam List<String> codes, HttpServletRequest request) {

        Map<String, Object> result = iLocationBearTypeService.unactiveStatus(codes);
        Integer success = MapUtil.getInt(result, "success");
        List<String> useName = MapUtil.get(result, "useName", List.class);
        int failNum = codes.size() - success;
        StringBuilder builder = new StringBuilder();
        builder.append("停用成功" + success + "条；失败" + failNum + "条；");
        if(IterUtil.isNotEmpty(useName)){
           for(String m:useName){
               builder.append("<br><font color='red'>库位承载类型【"+m+"】停用失败，库位使用中;</font>");
           }
        }
        return Result.ok(builder.toString());

    }


    /**
     * 获取 库位承载类型下拉列表数据
     *
     * @param locationBearTypeDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 库位承载类型下拉列表数据", notes = "获取 库位承载类型下拉列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = WarehouseVO.class))
    @NeedLogin
    @GetMapping("/chooseData")
    public Result chooseData(LocationBearTypeDTO locationBearTypeDTO, HttpServletRequest request) {
        try {
            List<LocationBearTypeVO> locationBearTypeVOList = iLocationBearTypeService.findLocationBearType();
            return Result.ok().data(locationBearTypeVOList);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }
 }
